/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.google.code.teamtools.persist;

import java.io.Serializable;
import java.util.List;

/**
 * Generic interface for Hibernate DAO.
 * 
 * @author David Kuhn
 */
public interface IGenericDao<T, ID extends Serializable>
{
	/**
	 * Returns the number of persisted entities.
	 * 
	 * @return number of entities
	 */
	int count();

	/**
	 * Deletes the entity.
	 * 
	 * @param entity the entity to delete
	 */
	void delete(T entity);

	/**
	 * Retrieves all entities of type <code>T</code> from storage.
	 * 
	 * @return a <code>List</code> of entities
	 */
	List<T> retrieveAll();

	/**
	 * Finds all entities that exhibit the same properties as the entity 
	 * provided. Properties can optionally be ignored if they are not relevant
	 * to the query.
	 * 
	 * @param exampleInstance an entity to match
	 * @param excludeProperty any number of properties to exclude from the search
	 * @return a <code>List</code> of entities or null if no match found
	 */
	List<T> findByExample(T exampleInstance, String... excludeProperty);

	/**
	 * Attempts to find the entity having the specified ID. Returns the entity
	 * if found or <code>null</code>. 
	 * <br/><br/>
	 * <em><b>N.B.</b> This operation does not lock the entity being retrieved.</em>
	 * 
	 * @param id the ID of the entity to find
	 * @return the entity or <code>null</code>
	 * @see #findById(Serializable, boolean)
	 */
	T findById(ID id);

	/**
	 * Attempts to find the entity having the specified ID. Returns the entity
	 * if found or <code>null</code>. Optionally the entity can be locked upon
	 * retrieval.
	 * 
	 * @param id the ID of the entity to find
	 * @param lock true to lock the entity being retrieved, otherwise false
	 * @return the entity or <code>null</code>
	 * @see #findById(Serializable)
	 */
	T findById(ID id, boolean lock);

	/**
	 * Saves (creates) or updates the entity passed.
	 * 
	 * @param entity the entity to save or update
	 */
	void save(T entity);
}
